#include "Background.h"


CBackground::CBackground()
{
	ReadFileInputData();
}


void CBackground::ReadFileInputData(void)
{
	char *binary;
	long isize;

	FILE *file = fopen("resources\\bitmap.txt", "rb");


	if (!file)
	{
		printf("\nFile nguon co van de");
	}
	else
	{
		fseek(file, 0, SEEK_END);
		isize = ftell(file);
		fseek(file, 0, SEEK_SET);// fseek(file, size, 0);
		binary = new char[isize];

		fread(binary, 1, isize, file);
		fclose(file);

		int i = 0, j = 0;
		int k = 0;
		int sum = 0;

		while (binary[k] != NULL)
		{
			sum = 0;
			j;
			if (binary[k] > 47 && binary[k] < 58)
			{

				while (binary[k + 1] > 47 && binary[k + 1] < 58)
				{
					sum = sum * 10 + binary[k] - 48;
					k++;
				}
				sum = sum * 10 + binary[k] - 48;

				MAPDATA[i][j] = sum;

				if (++j >103)
				{
					i++;
					j = 0;
				}
			}
			k++;
		}
	}
}

void CBackground::Background_Init(LPDIRECT3DDEVICE9 _d3ddev, LPD3DXSPRITE &_spriteHandler, HINSTANCE hInstance, HWND hWnd)
{
	_background = new CSprite(_spriteHandler, L"resources\\bitmap.png", FRAMEWIDTH, FRAMEHEIGHT, 61, 8, D3DCOLOR_XRGB(255, 0, 255));
	CCamera::Instance->SetWorldSize(GAMEWORLDWIDTH, GAMEWORLDHEIGHT);
}
void CBackground::Background_Render(D3DXVECTOR2 contra)
{
	int vpx = contra.x - SCREEN_WIDTH / 2;
	if (vpx <= 0)
	{
		vpx = 0;
	}

	if (vpx >= GAMEWORLDWIDTH - SCREEN_WIDTH)
	{
		vpx = GAMEWORLDWIDTH - SCREEN_WIDTH;
	}

	for (int i = 0; i < MAPHEIGHT; i++)
	{
		for (int j = 0; j < MAPWIDTH; j++)
		{
			_background->Render(FRAMEWIDTH / 2 + (FRAMEWIDTH * j), FRAMEHEIGHT * (MAPHEIGHT - i) - FRAMEHEIGHT / 2, MAPDATA[i][j]);
		}
	}
}
void CBackground::Background_End()
{
	if (_background != NULL)
		delete _background;
}

CBackground::~CBackground()
{
}